<?php

require_once 'Connection.php';
require_once 'Functions.php';

class DanhDe {

    public $conn;

    function __construct() {
        $con = new Connection();
        $this->conn = $con->configDB();
    }

    function getFirstRegion() {
        mysqli_query($this->conn, "SET NAMES 'utf8'");
        $strQuery = mysqli_query($this->conn, "SELECT RegionID from regions");
        $row = mysqli_fetch_array($strQuery);
        return $row['RegionID'];
    }

    function getFirstThreadByRegionID($regionID) {
        mysqli_query($this->conn, "SET NAMES 'utf8'");
        $strQuery = mysqli_query($this->conn, "SELECT ThreadID from region_thread WHERE RegionID = '$regionID'");
        $row = mysqli_fetch_array($strQuery);
        return $row['ThreadID'];
    }

    function getListChooseRegion() {
        $func = new Functions();
        $currentDate = $func->getCurrentDate();
        $dayID = $currentDate["DayInWeek"];
        mysqli_query($this->conn, "SET NAMES 'utf8'");
        $strQuery = mysqli_query($this->conn, "CALL GetRegionsAndProvinceByDayID('$dayID')");
        $arr = array();
        while ($row = mysqli_fetch_array($strQuery)) {
            array_push($arr, $row);
        }
        return $arr;
    }

    function getListThread() {
        $regionID = (isset($_REQUEST['regionID'])) ? $_REQUEST['regionID'] : "";
        mysqli_query($this->conn, "SET NAMES 'utf8'");
        $strQuery = mysqli_query($this->conn, "CALL GetThreadTypeByRegionID('$regionID')");
        $arr = array();
        while ($row = mysqli_fetch_array($strQuery)) {
            array_push($arr, $row);
        }
        return $arr;
    }

    function getListChoiDe() {
        $regionID = (isset($_REQUEST['regionID'])) ? $_REQUEST['regionID'] : "";
        $threadID = (isset($_REQUEST['threadID'])) ? $_REQUEST['threadID'] : "";
        mysqli_query($this->conn, "SET NAMES 'utf8'");
        $strQuery = mysqli_query($this->conn, "CALL GetSubThreadByRegionIDandThreadID('$regionID', '$threadID')");
        $arr = array();
        while ($row = mysqli_fetch_array($strQuery)) {
            array_push($arr, $row);
        }
        return $arr;
    }

    function handleBet() {
        /* Kiểm tra tổng số tiền chơi với Tài khoản, Số tiền tối thiểu để chơi, Thời gian chơi */
        $func = new Functions();
        $currentDate = $func->getCurrentDate();

        $userLog = $func->getSessionLogin();
        $msg = NULL;
        $playerID = $userLog['PlayerID'];
        $betDate = $currentDate['Day'] . '-' . $currentDate['Month'] . '-' . $currentDate['Year'];
        $betTime = $currentDate['Hour'] . ':' . $currentDate['Minute'];
        $betH = $currentDate['Hour'];
        $betTimeAllow = intval($betH) * 60;
        $winTime = 0;

        $arr = array();
        $totalMoney = intval($_POST['TotalBetMoney']) * 1000;
        $arr = $_POST['BetInfo'];
        // kiem tra dang nhap chua
        if ($userLog == NULL)
            $msg = -1;
        // kiem tra choi truoc 16h      
        else if ($betTimeAllow > 959)
            $msg = 0;
        // kiem tra tien toi thieu cua loai choi
        else if (intVal($arr[0]['BetMoney']) * 1000 < $this->getMinMoneybet($arr[0]['SubThreadRuleID']))
            $msg = 1;
        // kiem tra tien con lai trong tai khoan co du choi khong
        else if ($totalMoney > intval($this->getCurrentBalance($playerID)))
            $msg = 2;
        else {
            for ($i = 0; $i < count($arr); $i++) {
                $subThreadRuleID = $arr[$i]['SubThreadRuleID'];
                if (substr($arr[$i]['BetNumber'], strlen($arr[$i]['BetNumber']) - 1, 1) == '-')
                    $betNumber = substr($arr[$i]['BetNumber'], 0, strlen($arr[$i]['BetNumber']) - 1);
                else
                    $betNumber = $arr[$i]['BetNumber'];
                $betMoney = intVal($arr[$i]['BetMoney']) * 1000;
                $provinceID = $arr[$i]['ProvinceID'];
                mysqli_query($this->conn, "SET NAMES 'utf8'");
                $strQuery = mysqli_query($this->conn, "CALL InsertBet($playerID,'$subThreadRuleID','$betDate','$betTime','$betNumber','$betMoney',$winTime,'$provinceID')");
                mysqli_next_result($this->conn);
                // cap nhat tien trong tai khoan khi choi    
                mysqli_query($this->conn, "CALL UpdateBalanceWhenBet(1,'$betMoney')");
                mysqli_next_result($this->conn);
            }
            // thanh cong
            $msg = 3;
        }
        return $msg;
    }

    function getCurrentBalance($playerID) {
        mysqli_query($this->conn, "SET NAMES 'utf8'");
        $strQuery = mysqli_query($this->conn, "SELECT BMoney FROM playerbalance WHERE PlayerID = '$playerID'");
        $row = mysqli_fetch_array($strQuery);
        return $row['BMoney'];
    }

    function getMinMoneybet($subThreadRuleID) {
        mysqli_query($this->conn, "SET NAMES 'utf8'");
        $strQuery = mysqli_query($this->conn, "SELECT MinMoneyBet FROM subthreadrule WHERE SubThreadRuleID = '$subThreadRuleID'");
        $row = mysqli_fetch_array($strQuery);
        return $row['MinMoneyBet'];
    }

}
